﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace SyncCommon
{
    public class ThreadSafeDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
    {
        private Dictionary<TKey, TValue> Container = new Dictionary<TKey, TValue>();


        public int Count
        {
            get
            {
                lock (this)
                {
                    return Container.Count;
                }
            }
        }

        public bool ContainKey(TKey key)
        {
            lock (this)
            {
                return Container.ContainsKey(key);
            }
        }

        public TValue this[TKey key]
        {
            get
            {
                lock (this)
                {
                    return Container[key];
                }
            }
            set
            {
                lock (this)
                {
                     Container[key] = value;
                }
            }
        }


        public void Add(TKey key, TValue value)
        { 
            lock(this)
            {
                Container.Add(key, value);
            }
        }

        public void Clear()
        {
            lock (this)
            {
                Container.Clear();
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return Container.GetEnumerator();
        }

        IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
        {
            return Container.GetEnumerator();
        }
    }
}
